import json
import logging
import logging.config
import os

DATA_DIR = '../data/'
FIRST_SHEET_NAME = '封皮'
SECOND_SHEET_NAME = '课程目标达成情况报告'
LOGS_DIR = '../logs/'  # 日志存储目录
OUT_DIR = '../outs/'  # 结果目录
LOGS_CONFIG_FILE = "../config/logconfig.json"  # 日志配置文件目录
OBJECTIVITY = 0  # 客观页帧类型
SUBJECTIVITY = 1  # 非客观页帧类型
SCORE_LABEL = 1  # 主观评价页帧的分数标记
CLASS_ID_SHEET_IDX = 2  # 默认第3个页帧会有班级,其他页帧没有班级

# 页帧计算结果键
SCORE_INFO = '分数计算结果'
SCORE_MEAN_INFO = '平均均分计算结果'
ACH_INFO = '达成度计算结果'
ACH_MEAN_INFO = '平均达成度计算结果'
CLASS_IDS = '班级计算结果'
OBJECTIVITY_NUM = '客观评价考核数'
GOAL_NUM = '考核目标数'

DIAGRAMS = '散点图计算结果'
REPORT_INFO = '报告计算结果'
SUMMARY_INFO = '总表计算结果'
STATISTIC_INFO = '达成人数统计计算结果'


# 根据df单元格的值获取满足条件的值在df中的位置
def loc_by_value(df, value):
    loc = df.where(df == value).stack().index
    df_copy = df.copy()
    df_copy.columns = range(df.shape[1])
    df_copy.reset_index(drop=True, inplace=True)
    iloc = df_copy.where(df_copy == value).stack().index
    return list(zip(iloc, loc))


# df行插入
def insert_row(df, loc, index, value, allow_duplicates=False):
    df = df.T
    df.insert(loc, index, value, allow_duplicates)
    return df.T


# 日志配置引用
def setup_logging(config_file_path, default_level=logging.DEBUG):
    path = config_file_path
    if not os.path.isdir(LOGS_DIR):
        os.makedirs(LOGS_DIR)
    if os.path.exists(path):
        with open(path, "r") as f:
            config = json.load(f)
        logging.config.dictConfig(config)
    else:
        logging.basicConfig(level=default_level)


setup_logging(LOGS_CONFIG_FILE)
logger = logging.getLogger('my_loger')
